<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Auth_Controller extends CI_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->model('admin_model');
		session_start();
		$this->template->set_template('admin');
    }
	
	function index(){
		if(isset($_SESSION['is_admin_logged_in'])){
			$this->html_meta['robots'] = 'nofollow,noindex';
			$this->template->write_view('header', 'admin_header_block', NULL, FALSE);
			if($_SESSION['current_admin_page'] == "category"){
				$this->template->write_view('content', 'admin_category_block', array('category' => $this->admin_model->get_category_lists()), FALSE);
			}else{
				$this->template->write_view('content', 'admin_product_block', array('product' => $this->admin_model->get_product_lists()), FALSE);
			}
			$this->render_template = TRUE;
		}else{
			$this->html_meta['robots'] = 'nofollow,noindex';
			$this->template->write_view('header', 'admin_header_block', NULL, FALSE);
			$this->template->write_view('content', 'admin_login_block', null, FALSE);
			$this->render_template = TRUE;
		}
	}
	
	function login(){
		$this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
		$this->form_validation->set_rules('password', 'Password', 'required|callback_check|xss_clean');
		
		if($this->form_validation->run()== FALSE){
			$this->html_meta['robots'] = 'nofollow,noindex';
			$this->template->write_view('header', 'admin_header_block', NULL, FALSE);
			$this->template->write_view('content', 'admin_login_block', null, FALSE);
			$this->render_template = TRUE;
			
		}else{
			if($query = $this->admin_model->admin_login()){
				$_SESSION['is_admin_logged_in'] = TRUE;
				$this->html_meta['robots'] = 'nofollow,noindex';
				$this->template->write_view('header', 'admin_header_block', NULL, FALSE);
				if($_SESSION['current_admin_page'] == "category"){
				$this->template->write_view('content', 'admin_category_block', array('category' => $this->admin_model->get_category_lists()), FALSE);
				}else{
					$this->template->write_view('content', 'admin_product_block', array('product' => $this->admin_model->get_product_lists()), FALSE);
				}
			$this->render_template = TRUE;
			}
		}
	}
	
	function check(){
		if($query = $this->admin_model->admin_login()){
			return TRUE;
		}else{
			$this->form_validation->set_message('check','Invalid username or password');
			return FALSE;
		}
	}
	
	function logout(){
		unset($_SESSION['is_admin_logged_in']);
		//header("Location:");
		$this->html_meta['robots'] = 'nofollow,noindex';
		$this->template->write_view('header', 'admin_header_block', NULL, FALSE);
		$this->template->write_view('content', 'admin_login_block', NULL, FALSE);
		$this->render_template = TRUE;
	}
}